/*
 * HideMe: hide files on a cellphone
 * Copyright (C) 2009  Eduard Martínez
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package hideme;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.rms.*;

public class FileList extends Hashtable{
    private final static String STORE_NAME = "HideMeFiles";
    private boolean init = false;

    private void init(){
        try {
            RecordStore store = RecordStore.openRecordStore(STORE_NAME, true);
            RecordEnumeration e = store.enumerateRecords(null, null, false);
            byte aux[];
            int recordId;
            while(e.hasNextElement()){
                recordId = e.nextRecordId();
                aux = store.getRecord(recordId);
                super.put(new Integer(recordId), new String(aux,0,aux.length));
            }
            store.closeRecordStore();
        }catch (RecordStoreException ex) {
            throw new RuntimeException(ex.getMessage());
        }
        init = true;
    }
    
    public boolean isDir(String fileName){
        return  fileName.endsWith("/");
    }

    public boolean isDir(int recordId){
        if(!init)
            init();
        String fileName = (String) get(new Integer(recordId));
        return  fileName != null && fileName.endsWith("/");
    }

     public String get(int recordId){
         if(!init)
             init();
         return (String)get(new Integer(recordId));
     }

     public String remove(int recordId){
         return (String) remove(new Integer(recordId));
     }

     public Object remove(Object arg){
         if(!init)
             init();
         if(arg instanceof Integer){
            try {
                RecordStore store = RecordStore.openRecordStore(STORE_NAME, true);
                store.deleteRecord(((Integer)arg).intValue());
                store.closeRecordStore();          
            } catch (RecordStoreException ex) {
                throw new RuntimeException(ex.getMessage());
            }
            return super.remove(arg);
         }else{
            throw new RuntimeException("Only integers allowed");
         }
     }

     public int add(String fileName){
         byte[] data = fileName.getBytes();
         int ret;
         try {
             RecordStore store = RecordStore.openRecordStore(STORE_NAME, true);
             ret = store.addRecord(data, 0, data.length);
             store.closeRecordStore();
         } catch (RecordStoreException ex) {
             throw new RuntimeException(ex.getMessage());
         }
         super.put(new Integer(ret), fileName);
         return ret;
     }

     public Object put(Object key, Object value){
        throw new IllegalArgumentException("Put not allowed");
     }

     public Enumeration elements(){
         if(!init)
             init();
         return super.elements();
     }

     public Enumeration keys(){
         if(!init)
             init();
         return super.keys();
     }
}